package com.kangdi.riggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kangdi.riggie.common.Result;
import com.kangdi.riggie.entity.Employee;
import com.kangdi.riggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @PostMapping("login")
    public Result<Employee> login(@RequestBody Employee employee, HttpSession session){
        //将由页面来的密码进行加密

        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名从数据库中查询
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username",employee.getUsername());
        Employee employee1 = employeeService.getOne(wrapper);
        if (employee1 ==null){
            return Result.error("该用户不存在");
        }
        if (!employee1.getPassword().equals(password)){
            return Result.error("密码错误");
        }
        if (employee1.getStatus()==0){
            return Result.error("该用户已被禁用");
        }
        session.setAttribute("employee",employee1.getId());
        return Result.success(employee1);
    }
    @RequestMapping("logout")
    public Result<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return Result.success("退出成功");
    }
    @RequestMapping
    public Result<String> save(@RequestBody Employee employee,HttpSession session){
      log.info("新增员工信息为:"+employee);
      employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        boolean save = employeeService.save(employee);
        if (save){
            return Result.success("新增成功");
        }else {
            return Result.error("新增失败");
        }
    }

    /**
     * 分页展示员工
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page> page(int page,int pageSize,String name){
        Page<Employee> result = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
        employeeService.page(result,lambdaQueryWrapper);
        return Result.success(result);
    }
    @PutMapping
    public Result<String> update(@RequestBody Employee employee,HttpSession session){
        boolean b = employeeService.updateById(employee);
        if (b){
            return Result.success("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }
    @GetMapping("{id}")
    public Result<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee!=null){
            return Result.success(employee);
        }
        return Result.error("没有找到");
    }
}
